package org.csu.lestore.api.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Base64;

public class ImgUtil {
    public static String getImageBaseSrc(String name, String type)  {
        String filePath = System.getProperty("user.dir") + "\\src\\main\\resources\\static\\images\\";
        if (type != null && type.equals("user"))
            filePath += "user\\";
        else if(type != null && type.equals("product"))
            filePath += "product\\";
        else if(type != null && type.equals("category"))
            filePath += "category\\";
        else if(type != null && type.equals("item"))
            filePath += "item\\";
        else
            filePath += "else\\";
        String fileName = filePath + name;
        File file = new File(fileName);
        try {
            InputStream in = new FileInputStream(file);
            byte[] data = null;
            data = new byte[in.available()];
            in.read(data);
            in.close();
            Base64.Encoder encoder = Base64.getEncoder();
            return "data:image/jpeg;base64,"+encoder.encodeToString(data);
        } catch (IOException e) {
            e.printStackTrace();
        }
            return null;
    }
}

